
insert into asm_e_c_simplicitta
select ec.az_id,
 ec.MOV_NUM,
 ec.SCR_IMP_D,
 ec.SCR_IMP_A,
 ec.PAR_ID,
 ra.rata_num,
 ra.rata_imp,
        ec.SOG_ID,
        ec.CSI_IDCLI,
        ec.SOG_RAGSOC,
        ec.SCR_RIFERIMENTO,
        ec.ISS_ID_PARTITA,
        ec.tmov_id,
        ec.DST_ID,
       ec.TSCR_DES,
        ec.Tmov_des,
        ra.rata_dtsca,
        
        ec.MOV_DT,
        ra.inc_dtpag,
        substr(ra.RATA_OCR,1,16) ocr,
       ec.mov_nota,
       ec.scrdst_num
 
-- ec.*,to_char(ra.rata_dtsca,'dd/mm/yyyy') data_scadenza_rata
    from V_SCRITTURADEST_EC_tmp ec,v_rata ra
where ec.az_id = '01'
and   ra.az_id(+) = ec.az_id
and   ra.par_id(+)=ec.par_id 
and   ra.dil_staidsys(+) = 2
--and   ec.par_id = 389355
AND  EC.ISS_ID_PARTITA =602 
and  EC.MOV_DT > sysdate -800  --stato partita 602 aperta 603 chiusa
AND     EC.DST_ID in( 1,18)
and   ec.tmov_id<> 1 
and   ec.tmov_id<> 3
and   ec.tmov_id<> 4
--and ec.par_id =1054842


union

select ec.az_id,
 ec.MOV_NUM,
 ec.SCR_IMP_D,
 ec.SCR_IMP_A,
 ec.PAR_ID,
 ra.rata_num,
 ra.rata_imp,
        ec.SOG_ID,
        ec.CSI_IDCLI,
        ec.SOG_RAGSOC,
        ec.SCR_RIFERIMENTO,
        ec.ISS_ID_PARTITA,
        ec.tmov_id,
        ec.DST_ID,
        ec.TSCR_DES,
 case when inc_dtpag is not null then 'INCASSO RATA' else case when rata_imp is null then ' IMPORTO FATTURA' else 'IMPORTO RATA' end end xx,
        rata_dtsca,
        
        ec.MOV_DT,
        ra.inc_dtpag,
        substr(ra.RATA_OCR,1,16) ocr,ec.mov_nota, ec.scrdst_num
        

    from V_SCRITTURADEST_EC_tmp ec,v_rata ra
where ec.az_id = '01'
and   ra.az_id(+) = ec.az_id
and   ra.par_id(+)=ec.par_id 
and   ra.dil_staidsys(+) =2

AND  EC.ISS_ID_PARTITA =602 
and  EC.MOV_DT > sysdate -800  --stato partita 602 aperta 603 chiusa
AND     EC.DST_ID in( 1,18)
and  ec.tmov_id= 1
--and ec.par_id in( 1054842)
           
UNION 


select ec.az_id,
 ec.MOV_NUM,
 ec.SCR_IMP_D,
 ec.SCR_IMP_A,
 ec.PAR_ID,
 ra.rata_num,
 ra.rata_imp,
        ec.SOG_ID,
        ec.CSI_IDCLI,
        ec.SOG_RAGSOC,
        ec.SCR_RIFERIMENTO,
        ec.ISS_ID_PARTITA,
        ec.tmov_id,
        ec.DST_ID,
        ec.TSCR_DES,
         ec.Tmov_DES, 
        rata_dtsca,
        
        ec.MOV_DT,
        ra.inc_dtpag,
        substr(ra.RATA_OCR,1,16) ocr,ec.mov_nota, ec.scrdst_num
 
-- ec.*,to_char(ra.rata_dtsca,'dd/mm/yyyy') data_scadenza_rata
    from V_SCRITTURADEST_EC_tmp ec,v_rata ra
where ec.az_id = '01'
and   ra.az_id(+) = ec.az_id

and   ra.par_id(+)=ec.par_id 
and   ra.dil_staidsys(+) = 2

and   ec.tmov_id= 1 

AND  EC.ISS_ID_PARTITA =603 
 --stato partita 602 aperta 603 chiusa
AND     EC.DST_ID in( 1,3)

--and ec.par_id =1054842;

